<template>
  <v-app-bar title="巡检机器人">
    <template #append>
      <v-btn class="mx-2" color="indigo-darken-3" variant="flat" @click="startTask">开始任务</v-btn>
      <v-btn class="mx-2" color="error" variant="flat" @click="quitTask">结束任务</v-btn>
      <v-chip
        class="mx-2"
        :color="connect ? 'success' : 'error'"
        label
        size="small"
      >
        {{ connect ? '已连接' : '未连接' }}
      </v-chip>
    </template>
  </v-app-bar>
</template>

<script setup lang="ts">
  import type { PoIType } from '@/typings'
  import { goalNavigate, missionNavigate } from '@/api/services'
  import { MISSION_FUNC_TYPE, MISSION_TYPE, MOTION_TYPE, NAV_FUNC_TYPE, NAV_MODE } from '@/constant'
  import { useAppStore } from '@/stores/app'
  const appStore = useAppStore()
  const { connect } = storeToRefs(appStore)

  function quitTask () {
    if (!appStore.connect) {
      return
    }
    MissionNavigateServiceCall(MISSION_FUNC_TYPE.CANCLE, appStore.taskType, appStore.nav_mode_poi)
    GoalNavigateServiceCall(NAV_FUNC_TYPE.CANCLE, appStore.nav_mode_goal)
  }

  function startTask () {
    if (!appStore.connect) {
      return
    }
    if (appStore.taskType === -1) {
      // ElMessage({
      //   message: t('task.noNavigationModeSelected'),
      //   type: 'error',
      //   grouping: true,
      // })
      return
    }
    // 定点导航
    if (appStore.taskType === MISSION_TYPE.LOCATION) {
      GoalNavigateServiceCall(NAV_FUNC_TYPE.START, appStore.nav_mode_goal)
      return
    }

    switch (appStore.taskType) {
      case MISSION_TYPE.NORMAL_POI: {
        MissionNavigateServiceCall(MISSION_FUNC_TYPE.START, appStore.taskType, appStore.nav_mode_poi)
        break
      }
      case MISSION_TYPE.MULTI_POI: {
        MissionNavigateServiceCall(
          MISSION_FUNC_TYPE.START,
          appStore.taskType,
          appStore.nav_mode_cruise,
        )
        break
      }
      case MISSION_TYPE.HOME_MISSION: {
        MissionNavigateServiceCall(
          MISSION_FUNC_TYPE.START,
          appStore.taskType,
          appStore.nav_mode_charge,
        )
        break
      }
    }
  }

  async function GoalNavigateServiceCall (value: number, nav_mode_value: number) {
    const requset_goal_nav = {
      cmd: {
        func_type: NAV_FUNC_TYPE.START,
        nav_mode: NAV_MODE.FREE,
        goal_pose: {
          x: 0,
          y: 0,
          theta: 0,
        },
      },
    }

    switch (value) {
      case NAV_FUNC_TYPE.CANCLE: {
        requset_goal_nav.cmd.func_type = NAV_FUNC_TYPE.CANCLE
        try {
          const result = await goalNavigate(requset_goal_nav)
          if (result.ret) {
          // ElMessage({
          //   message: '导航取消导航指令成功',
          //   type: 'success',
          //   grouping: true,
          // })
          } else {
            throw new Error('导航取消导航指令失败')
          }
        } catch {
          // ElMessage({
          //   message: error as string,
          //   type: 'error',
          //   grouping: true,
          // })
        }

        break
      }
    }
  }

  async function MissionNavigateServiceCall (
    func_type: number,
    mission_type: number,
    motion_type: number,
  ) {
    const requset_mission_nav = {
      cmd: {
        func_type: MISSION_FUNC_TYPE.START,
        mission_type: MISSION_TYPE.NORMAL_POI,
        motion_type: MOTION_TYPE.TRACK_GOTO,
        poi_array: {
          pois: [] as PoIType[],
        },
        loop_num: 1,
      },
    }

    switch (func_type) {
      case MISSION_FUNC_TYPE.START: {
        requset_mission_nav.cmd.func_type = func_type
        requset_mission_nav.cmd.mission_type = mission_type
        if (motion_type !== -1) {
          requset_mission_nav.cmd.motion_type = motion_type
        }
        requset_mission_nav.cmd.poi_array.pois.length = 0

        // POI 导航
        if (mission_type === MISSION_TYPE.NORMAL_POI) {
          if (appStore.selectedPoi.length === 0) {
            // ElMessage({
            //   message: t('task.pleaseSelectNavigationPoint'),
            //   type: 'warning',
            //   grouping: true,
            // })
            return
          }
          requset_mission_nav.cmd.poi_array.pois = [appStore.selectedPoi[0]!]
        }

        try {
          const result = await missionNavigate(requset_mission_nav)

          if (result.ret) {
            // ElMessage({
            //   message: t('task.navigationSentSuccess'),
            //   type: 'success',
            //   grouping: true,
            // })
          } else {
            throw new Error('导航发送失败')
          }
        } catch {
          // ElMessage({
          //   message: error as string,
          //   type: 'error',
          //   grouping: true,
          // })
        }

        break
      }
      case MISSION_FUNC_TYPE.CANCLE: {
        requset_mission_nav.cmd.func_type = MISSION_FUNC_TYPE.CANCLE
        try {
          const result = await missionNavigate(requset_mission_nav)

          if (result.ret) {
            // ElMessage({
            //   message: t('task.cancelNavigationSuccess'),
            //   type: 'success',
            //   grouping: true,
            // })
          } else {
            throw new Error('取消导航指令失败')
          }
        } catch {
          // ElMessage({
          //   message: error as string,
          //   type: 'error',
          //   grouping: true,
          // })
        }

        break
      }
    }
  }
</script>
